<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/3/21
 * Time: 14:50
 */

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;
class Monitor extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('monitor')
            ->setDescription('the rundata command');
    }

    protected function execute(Input $input, Output $output)
    {
        try {
            // 指令输出
            $monitor = cache('monitor');
            $keyword = Db::name('web_keyword')->select();
            // $keyword = cache('keyword');
            // $website = cache('website');
            $website = Db::name('web')->select();
            if ($monitor['hover1'] != date('H') && $monitor['hover2'] != date('H')) return;
            if (empty($keyword)) return;
            if (empty($website)) return;
            $keyword = array_column($keyword->toArray(), 'keyword');
            $values = [];
            foreach ($website AS $key => $value) {
                $web = str_replace(['https://','http://'],'',$value['web']);
                $res = curl($web, [], false);
                if (is_array($res)) {
                    $values[] = [
                        'website' => $value['web'],
                        'name' => $value['name'],
                        'keywords' => '检查到网站无法访问'
                    ];
                }else {
                    $keywords = [];
                    foreach ($keyword AS $index => $item) {
                        if (strpos($res, $item) !== false) $keywords[] = $item;
                    }
                    if (!empty($keywords))
                        $values[] = [
                            'website' => $value['web'],
                            'name' => $value['name'],
                            'keywords' => '检查到触发关键词(' . implode(',', $keywords) . ')'
                        ];
                }
            }
            $string = '';
            foreach ($values AS $key => $value)
                $string .= '<tr><td>' . $value['name'] . '</td><td><a href="'.$value['website'].'">' . $value['website'] . '</a></td><td>'.$value['keywords'].'</td></tr>';
            $body = file_get_contents(public_path() . '/../view/admin/email.html');
            $body = str_replace('{content}', $string, $body);
            if (!empty($values)) {
//                $this->send('429872886@qq.com', '域名触发关键词', $body);
                $this->send('1213378232@qq.com', '域名触发关键词', $body);
                $this->send('243852058@qq.com', '域名触发关键词', $body);
                $this->send('65370042@qq.com', '域名触发关键词', $body);
            }
            die('ok');
        }catch (\Exception $e){
            $output->writeln($e->getTraceAsString());
            die;
        }
    }

    private function send($email,$title = '',$body = ''){
        $message = new \Swift_Message($title);
        $message = $message->setFrom('ppf1788@sina.com');
        $message = $message->setTo($email);
        $message = $message->setBody($body,'text/html' );
        $transport = new \Swift_SmtpTransport('smtp.sina.com', 465,'ssl');
        $transport = $transport->setUsername('ppf1788@sina.com');
        $transport = $transport->setPassword('14c1f2883d0b344e');
        $mailer = new \Swift_Mailer($transport);
        $mailer->send($message);
        //return json(['code' => 200,'msg' => '成功','data' => $res],200);
    }
}
